package cn.ad.server.persistence.dao;

import cn.ad.server.domain.ad.AuditPassedMarker;
import cn.ad.server.domain.ad.Creative;
import cn.ad.server.persistence.typehandler.EnumStatusTypeHandler;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface CreativeDao {

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "campaign_id", property = "campaign.id"),
			@Result(column = "campaign_id", property = "campaignId"),
			@Result(column = "ad_format", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			@Result(column = "ad_template_id", property = "adTemplate.id"),
			@Result(column = "impression_tracking", property = "impressionTracking"),
			@Result(column = "clicks_tracking", property = "clicksTracking"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType") ,
			@Result(column = "interaction_id", property = "interactionId"),
			//@Result(column = "id",property ="platformIds",many = @Many(select = "findCreativeAuditPlatform" ,fetchType = FetchType.DEFAULT)),
			@Result(column = "id",property ="AuditPassedMarkers",many = @Many(select = "findCreativeAuditPlatform" ,fetchType = FetchType.DEFAULT))
			/*@Result(column = "id",property ="creativeGroup",one = @One(select = "cn.ad.server.persistence.dao.CreativeGroupDao.selectByCreativeId" ,fetchType = FetchType.DEFAULT))*/
	})
	@Select("select * from creative ")
	List<Creative> findAll();
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "platform_id", property = "platformId"),
			@Result(column = "audit_id", property = "auditId")
	})
	@Select("select id,platform_id,audit_id from ad_material_audit where creative_id = #{id} and audit_status=2 and status=1 and material_id is null")
	List<AuditPassedMarker> findCreativeAuditPlatform(int id);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "campaign_id", property = "campaign.id"), 
			@Result(column = "campaign_id", property = "campaignId"),
			@Result(column = "ad_format", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			@Result(column = "ad_template_id", property = "adTemplate.id"),
			@Result(column = "impression_tracking", property = "impressionTracking"), 
			@Result(column = "clicks_tracking", property = "clicksTracking"), 
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType") ,
			@Result(column = "interaction_id", property = "interactionId"),
			@Result(column = "id",property ="AuditPassedMarkers",many = @Many(select = "findCreativeAuditPlatform" ,fetchType = FetchType.DEFAULT))
			/*@Result(column = "id",property ="creativeGroup",one = @One(select = "cn.ad.server.persistence.dao.CreativeGroupDao.selectByCreativeId" ,fetchType = FetchType.DEFAULT))*/
	})
	@Select("select * from creative where update_time >= #{value}")
	List<Creative> findAfterTime(String warmUpTime);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "campaign_id", property = "campaign.id"), 
			@Result(column = "campaign_id", property = "campaignId"),
			@Result(column = "ad_format", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			@Result(column = "ad_template_id", property = "adTemplate.id"),
			@Result(column = "impression_tracking", property = "impressionTracking"), 
			@Result(column = "clicks_tracking", property = "clicksTracking"), 
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType") ,
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from creative where id = #{value}")
	Creative findById(long id);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "campaign_id", property = "campaign.id"), 
			@Result(column = "campaign_id", property = "campaignId"),
			@Result(column = "ad_format", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			@Result(column = "ad_template_id", property = "adTemplate.id"),
			@Result(column = "impression_tracking", property = "impressionTracking"), 
			@Result(column = "clicks_tracking", property = "clicksTracking"), 
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType") ,
			@Result(column = "interaction_id", property = "interactionId"),
			@Result(column = "id",property ="AuditPassedMarkers",many = @Many(select = "findCreativeAuditPlatform" ,fetchType = FetchType.DEFAULT))
			/*@Result(column = "id",property ="creativeGroup",one = @One(select = "cn.ad.server.persistence.dao.CreativeGroupDao.selectByCreativeId" ,fetchType = FetchType.DEFAULT))*/
	})
	@Select("select * from creative where id = #{id} and status = #{status}")
	Creative selectById(@Param("id")long id, @Param("status")int status);

}
